package com.sxjh.controller.pub;

import com.sxjh.common.BaseController;
import com.sxjh.common.Result;
import com.sxjh.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件处理
 */
@RestController
@RequestMapping("/api/file")
@Api(tags = "文件处理")
public class FileController extends BaseController {

  @Autowired
  private FileService fileService;

  /**
   * 文件上传
   */
  @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
  @ApiOperation("上传文件")
  @ApiImplicitParam(name = "file", value = "文件上传",required = true, dataTypeClass = MultipartFile.class)
  public Result upload(@ApiParam(hidden = true) MultipartFile file) throws Exception{
    if (file == null) return ok("文件不能为空");
    String uuid = fileService.saveFile(file);
    return ok(uuid);
  }

  /**
   * 文件下载
   */
  @GetMapping("/{id}")
  @ApiOperation("下载文件")
  public ResponseEntity<FileSystemResource> downloadFile(@PathVariable String id) {
    return fileService.download(id);
  }

}
